from jdk.openAi.impl import gpt, gpt_2
import random
import string
import log.logger as log
import time


# 根据文案及gif提供的信息匹配
def get_open_ai_to_title(template_id, text_parm, selected_gifs):
    text, system_prompt, count = create_text_and_system(template_id, text_parm, selected_gifs)
    while True:
        try:
            response_data,emos = gpt_2.response_string_json(text, system_prompt)
            log.log_put(12, None, None)
            log.log_put(None, None, response_data)
            if len(response_data) < count:
                log.log_put(13, None, None)
                log.log_put(None, None, f"{response_data}")
                break
            break
        except Exception as e:
            sleep_out(e)

    return response_data, emos


def get_open_ai_to_text(parm):
    text, system_prompt = create_context(parm)
    while True:
        try:
            response_data = gpt_2.response_string(text, system_prompt)
            break
        except Exception as e:
            # 处理异常的代码，例如打印异常信息
            sleep_out(e)

    return response_data


def sleep_out(msg):
    error_sleep_max = 5
    # 处理异常的代码，例如打印异常信息
    log_message = f"<<<OpenAi返回错误：{msg}\n将在 {error_sleep_max} 秒后重试....."
    log.log_put(None, None, log_message)

    time_remaining = error_sleep_max - 1
    while time_remaining > 0:
        log.log_put(None, None, f" {time_remaining} 秒后进行重试")
        time.sleep(1)
        time_remaining -= 1


def create_text_and_system(template_id, text_parm, selected_gifs):
    random_number = random.randint(2, 20)
    random_letter_random = ''.join(random.choice(string.ascii_letters) for _ in range(random_number))
    text = ""
    count = 50
    # 提示词
    di = random.randint(2, 5)
    title_msg = ""
    if di == 2:
        title_msg = f"{di}个标签，如 ['可爱','打呼']"
    elif di == 3:
        title_msg = f"{di}个标签，如 ['可爱','打呼','无敌']"
    elif di == 4:
        title_msg = f"{di}个标签，如 ['可爱','打呼','呆萌','无敌']"
    elif di == 5:
        title_msg = f"{di}个标签，如 ['可爱','打呼','呆萌','情侣','无敌']"
    system_prompt = f"我将给你一些标签结合给你的字母联想出一句话(title)可以是随机类型，并且生成{title_msg}(label_list)。不能出现（新年相关，表情包，字母，英文）及标点符号(，)。要具有互动性质结合目前网络热梗及创新，以10岁的智商及普遍的话术来回答我,字数必须要在12~20个字之间，只能返回一个，最后按照严格的JSON格式返回给我。"
    if template_id == 47:
        system_prompt = f"提供一些gif的信息给你,你的任务是给我关于粉丝互动及吃货的标题,要求简短,不能出现gif字样,所有gif综合起来只需要一个标题，最后按照严格的JSON格式返回给我。参数key = title"

    if text_parm is not None and text_parm['id'] != '无':
        text = f"结合提示:{text_parm['writing']} 情绪:{text_parm['emotion']},场景:{text_parm['scene']},"
    if len(selected_gifs) > 0:
        for index, gif in enumerate(selected_gifs, start=1):
            # 在这里添加适当的字符串拼接逻辑，例如：
            text += f"gif的第{index}个情绪:{gif['emotion']},标题:{gif['title']}，"

    return text + random_letter_random, system_prompt, count


def create_context(parm):
    if parm['id'] == 47:
        system_prompt = "根据提供的数字字母连想菜名，菜名中不能出现数字跟字母除非还真的有这个菜，返回一个json对象,内容放在property属性里面,property是个列表,要求列表不能重复,菜系哪个国家的都可以，但是必须返回中文"
        random_number = random.randint(1, 10000)
        random_number2 = random.randint(2, 20)
        random_letter = ''.join(random.choice(string.ascii_letters) for _ in range(random_number2))
        text = f"基于数字{random_number}及字母{random_letter}，随机生成{parm['number']}个菜名，确保每次结果不同。"
        return text, system_prompt
    else:
        print("1111")


if __name__ == '__main__':
    print(get_open_ai_to_text({"id": 47, "number": 6}))
